//
//  TextViewWithPlaceholder.swift
//  letuofu
//
//  Created by Julien Yan on 2020/8/4.
//  Copyright © 2020 WH-JS. All rights reserved.
//

import Foundation
import UIKit
import SnapKit

class TextViewWithPlaceHolder : UITextView,UITextViewDelegate {
    
    let placeHolderView = UILabel()
    
    init() {
        super.init(frame: .zero,textContainer: nil)
        self.delegate = self
    }
    
    func setPlaceHolder(text:String?, fontSize:CGFloat,fontWeight:UIFont.Weight = .regular ,textColor:UIColor,
                        _ closure: (_ make: ConstraintMaker) -> Void){
        self.drawLabel(label: placeHolderView, text: text, fontSize: fontSize, fontWeight: fontWeight, textColor: textColor, closure)
        placeHolderView.isUserInteractionEnabled = false
    }
    
    func textViewDidChange(_ textView: UITextView){
        let text:String? = textView.text.trim().nilIfEmpty
        if text == nil {
            placeHolderView.isHidden = true
        }
    }
    
    func textViewDidBeginEditing(_ textView: UITextView) {
        placeHolderView.isHidden = true
    }
    
    func textViewDidEndEditing(_ textView: UITextView) {
        let text:String? = textView.text.trim().nilIfEmpty
        placeHolderView.isHidden = (text != nil)
//        centerVerticalText()
    }
    
//    func centerVerticalText() {
//        let fitSize = CGSize(width: bounds.width, height: CGFloat.greatestFiniteMagnitude)
//        let size = sizeThatFits(fitSize)
//        let calculate = (bounds.size.height - size.height * zoomScale) / 2
//        let offset = max(1, calculate)
//        contentOffset.y = -offset
//    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
